import subprocess
import os
 
def acl_run(op_type, inputs_path, inputs_info, outputs_info, attr, device_id, op_dir, is_dynamic=0, inplace_input_index={}, run_profiling_mode="acl"):
    if run_profiling_mode == "acl":
        cmd = ["python3", "tools/acl_execute.py"]
        params = ["--op_type=%s" % op_type, "--inputs_path=%s" % str(inputs_path), "--inputs_info=%s" % str(inputs_info),
                "--outputs_info=%s" % str(outputs_info), "--attrs=%s" % str(attr), "--device_id=%s" % device_id,
                "--op_model_path=%s" % op_dir, "--is_dynamic=%s" % is_dynamic, "--inplace_input_index=%s" % inplace_input_index,
                "--run_profiling_mode=%s" % run_profiling_mode]
        cmd.extend(params)
        try:
            subprocess.run(cmd, timeout=60000)
        except Exception as e:
            print(e)
    elif run_profiling_mode == "on":
        print("msopprof (MindStudio Profiler For Operator) is part of MindStudio Operator-dev Tools. For any question, you can contact wx:zcc970602.")
        cmd = ["python3", "tools/acl_execute.py"]
        params = ["--op_type=%s" % op_type, '--inputs_path="%s"' % str(inputs_path), '--inputs_info="%s"' % str(inputs_info),
                '--outputs_info="%s"' % str(outputs_info), '--attrs="%s"' % str(attr), "--device_id=%s" % device_id,
                '--op_model_path=%s' % op_dir, "--is_dynamic=%s" % is_dynamic, '--inplace_input_index="%s"' % inplace_input_index,
                "--run_profiling_mode=%s" % str(run_profiling_mode)]
        cmd.extend(params)
        with open('tmp.sh', 'w', encoding='utf-8') as f:
            f.write(' '.join(cmd))
        tmp = ' '.join(cmd)
        os.chmod('tmp.sh', 0O700)
        try:
            subprocess.run(['msprof', 'op', '--output=%s'%op_dir, 'tmp.sh'], timeout=60000)
            os.remove('tmp.sh')
        except Exception as e:
            print(e)
    elif run_profiling_mode == "sim":
        sim_path = os.environ["LD_LIBRARY_PATH"]
        if "tools/simulator/Ascend" not in sim_path:
            print("\033[31m[> 错误 <]\033[0m", "仿真调试需要使能环境变量,如:export LD_LIBRARY_PATH=${ASCEND_TOOLKIT_HOME}/tools/simulator/Ascend910B2/lib/:$LD_LIBRARY_PATH")
            sys.exit(1)
        print("msopprof (MindStudio Profiler For Operator) is part of MindStudio Operator-dev Tools. For any question, you can contact wx:zcc970602.")
        cmd = ["python3", "tools/acl_execute.py"]
        params = ["--op_type=%s" % op_type, '--inputs_path="%s"' % str(inputs_path), '--inputs_info="%s"' % str(inputs_info),
                '--outputs_info="%s"' % str(outputs_info), '--attrs="%s"' % str(attr), "--device_id=%s" % device_id,
                '--op_model_path=%s' % op_dir, "--is_dynamic=%s" % is_dynamic, '--inplace_input_index="%s"' % inplace_input_index,
                "--run_profiling_mode=%s" % run_profiling_mode]
        cmd.extend(params)
        with open('tmp.sh', 'w', encoding='utf-8') as f:
            f.write(' '.join(cmd))
        tmp = ' '.join(cmd)
        os.chmod('tmp.sh', 0O700)
        try:
            subprocess.run(['msprof', 'op', 'simulator', '--output=%s'%op_dir, 'tmp.sh'], timeout=60000)
            os.remove('tmp.sh')
        except Exception as e:
            print(e)
    else:
        print("\033[31m[> error <]\033[0m" + "你输入的run_profiling_mode有问题, 有三种选项:'acl'、'on'、'sim', 传入的是%s" % run_profiling_mode)
        sys.exit(1)